// pages/login/login.js
var that;
var util=require('../../utils/util.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getpic:true,
    base64:'',
    uname:'',
    ugender:'',
    uolder:'',
    usize:'',
    uphone:'',
    time:'',
    name:'',
    gender:'',
    older:'',
    size:'',
    phone:'',
    openid:'',
  },
  chooseimage:function(){
    wx.chooseImage({
      success: function(res) {
        that.setData({
          base64:wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64")
        })
      },
    })
  },
  quname:function(e){
    this.setData({
      uname: e.detail.value
     });
  },
  qugender:function(e){
    this.setData({
      ugender: e.detail.value
     });
  },
  quolder:function(e){
    this.setData({
      uolder: e.detail.value
     });
  },
  qusize:function(e){
    this.setData({
      usize: e.detail.value
     });
  },
  quphone:function(e){
    this.setData({
      uphone: e.detail.value
     });
  },

 submit:function(options){
      //调用云数据库
      var TIME = util.formatTime(new Date());
      this.setData({
      time: TIME,
      });
       var nickname=this.data.uname;
       var gender=this.data.ugender;
       var older=this.data.uolder;
       var size=this.data.usize;
       var phone=this.data.uphone;
       var base64=this.data.base64;
       var time=this.data.time;
       app.globalData.uname=nickname;
       app.globalData.ugender=gender;
       app.globalData.uolder=older;
       app.globalData.usize=size;
       app.globalData.uphone=phone;
       app.globalData.base64=base64;
       app.globalData.time=time;
       if(base64==''||size==''||phone=='')
       {
        wx.showToast({
          title: '请填好必填项',
        })
       }
       else{
        // this.setData({
        //   name:''
        //  });
        //  this.setData({
        //   gender:''
        //  });
        //  this.setData({
        //   older:''
        //  });
        //  this.setData({
        //   size:''
        //  });
        //  this.setData({
        //   phone:''
        //  });-
        //  this.setData({
        //   base64:''
        //  });
        wx.request({
          url: 'http://localhost:8080/addOldManList',//写自己的服务器
          method: "POST",
          header: { 
            "Content-Type": "application/json" 
            },
          data: {
            name:nickname,
            sex:gender,
            age:older,
            article:size,
            phone_number:phone,
            image:"base64",
            time_now:time,
            openid_old:this.data.openid
          },
         success:function(res){
           console.log(res.data);
           wx.showToast({
             title: '入库成功',
           })
         }
       })
      wx.cloud.database().collection("shibie").add({
        data:{
          nickname:nickname,
          gender:gender,
          older:older,
          size:size,
          phone:phone,
          base64:base64
        },
        success:function(res){
          wx.showToast({
            title: '入库成功',
          })
        }
      })
      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey
        if(res.code){
          console.log(res.code)
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',//微信服务器获取appid的网址 不用变
            method:'post',//必须是post方法
            data:{
              js_code:res.code,
              appid:'wx6823eb6b04587eef',//仅为实例appid
              secret:'e2387f22a548a798e6946ac812edff63',//仅为实例secret
              grant_type:'authorization_code'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            success:function(response){
              console.log(response.data)
              that.setData({
                openid:response.data.openid
              })
            }
          })
        }else{
          console.log("登陆失败");
        }
      }
    })
  },
})